﻿/*
 * User: artem
 * Date: 27.04.2011
 * Time: 19:18
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Client.GUI
{
	/// <summary>
	/// Description of AddEditForm.
	/// </summary>
	public partial class AddEditForm : Form
	{
		private const int default_add_id = -1;
			
		private Logic.Controller controller = Logic.Controller.Instance;
		private int id = default_add_id;

        public AddEditForm()
        {
            const string add_windows_title = @"Add new Event";

            InitializeComponent();

            this.Text = add_windows_title;
            cmbType.SelectedIndex = 0;
            cmbPeriodicity.SelectedIndex = 0;
        }

        public AddEditForm(int event_id)
        {
            const string window_title = @"Edit Event";

            InitializeComponent();

            this.Text = window_title;

            id = event_id;

            var current_event = controller.GetEvent(event_id);
            txtTitle.Text = current_event.Title;
            txtDescription.Text = current_event.Description;
            cmbType.SelectedIndex = (int)current_event.Type;
            cmbPeriodicity.SelectedIndex = (int)current_event.Periodicity;
            dateTimePickerDate.Value = current_event.Date;
            dateTimePickerStart.Value = current_event.NotificationStart;
            dateTimePickerEnd.Value = current_event.NotificationEnd;
            numericUpDownCount.Value = current_event.NotificationCount;
        }
		
		private void BtnCancleClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		private void BtnOkClick(object sender, EventArgs e)
		{
            var current_event = new
                                {
                                    Title = txtTitle.Text,
                                    Description = txtDescription.Text,
                                    Periodicity = cmbPeriodicity.SelectedIndex,
                                    Type = cmbType.SelectedIndex,
                                    Date = dateTimePickerDate.Value,
                                    NotificationStart = dateTimePickerStart.Value,
                                    NotificationEnd = dateTimePickerEnd.Value,
                                    NotificationCount = numericUpDownCount.Value,
                                };

            try
            {
                if (id == default_add_id)
                    controller.AddEvent(current_event);
                else
                    controller.UpdateEvent(id, current_event);
            }
            catch (Exception exp)
            {
                ErrorShower.Show(exp);
                return;
            }
			
			this.Close();
		}
	}
}
